package tb4coding.sprider4lagou;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tb4coding.sprider4lagou.dao.IResultDao;
import tb4coding.sprider4lagou.pojo.Detail;
import tb4coding.sprider4lagou.util.GetSessionFactory;
import tb4coding.sprider4lagou.util.JsoupUtil;
import tb4coding.sprider4lagou.util.UrlConstants;

public class DetailsMain {

	public static void main(String[] args) {
		SqlSession session = GetSessionFactory.getInstace().getSessionFactory().openSession();
		List<String> idList = session.getMapper(IResultDao.class).getIdList();
		System.out.println("[LOG]------共搜索到" + idList.size() + "条职位");
		int th = 1;
		for(String id : idList){
			System.out.println("[LOG]------开始爬取第" + th + "条职位");
			String url = UrlConstants.getPositionUrl(id);
			Document doc = JsoupUtil.getUrlHtml(url );
			Elements describeElements = doc.getElementsByClass("job_bt");
			StringBuilder describe = new StringBuilder();
			List<Element> elementList = describeElements.first().children();
			for(Element element : elementList){
				describe.append(element.ownText());
				
			}
			String address = doc.select("dl.job_company dd div").first().ownText().toString();
			String temptation = doc.select("dd.job_request p:nth-child(2)").first().ownText().toString();
			
			
			Detail detail = new Detail();
			detail.setPositionId(id);
			detail.setAddress(address);
			detail.setDescribe(describe.toString());
			detail.setTemptation(temptation);
			System.out.println(address);
			System.out.println("-------------------------------");
			System.out.println(describe.toString());
			System.out.println("-------------------------------");
			System.out.println(temptation);
			session.getMapper(IResultDao.class).addDetails(detail);
			break;
		}
		session.commit();
		session.close();
		
	}

}
